<?php
    header("Content-Type:text/html;charset=utf-8");
    //连接数据库
    require ('config.php');
    $data = json_decode(file_get_contents("php://input"), true);
    $fun = $data['fun'];
    
    if($fun == 'chaxeng'){
        //查询
        $fid = $data['fid'];
        
        $validWidPattern = '/^\d+$/';
        if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
        	echo  json_encode(array('stat' => '400','msg' => '非法请求，GET数据错误！'),JSON_UNESCAPED_UNICODE);
        	exit();
        } 
        
        if($fid=='0' or !$fid){//没有请求，获取最新一条fid的数据
            $sql = "SELECT fid FROM gather ORDER BY fid DESC LIMIT 1";
            $fids = mysqli_query($conn,$sql);
            $fids = mysqli_fetch_array($fids);
            $fid = $fids['fid'];
        }
        $sql = "SELECT f.stoptime,f.pax,f.list,u.username,f.class,f.title,f.msg FROM gather f JOIN users u ON f.uid=u.uid WHERE f.fid LIKE ".$fid;
        $gather = mysqli_query($conn,$sql);
        $gather = mysqli_fetch_array($gather);
            if($gather){
                $stoptime = $gather['stoptime'];//2023-04-02T19:25
                $time1 = substr($stoptime,0,10).' '.substr($stoptime,11,5);
				date_default_timezone_set('PRC');
                $time2=date("Y-m-d H:i");
                //$stoptime = str_ireplace("-","/",$stoptime);
                $stoptime = substr($stoptime,5,2).'月'.substr($stoptime,8,2).'日'.substr($stoptime,-5,5);
				
                if($time1 <= $time2){
                    $stoptime = '收集表已过期';
                }
                $pax = $gather['pax'];
                $list = $gather['list'];
                $username = $gather['username'];
                $class = $gather['class'];
                $title = $gather['title'];
                $msg = $gather['msg'];
                if($msg == NULL){$msg='无';}
                if($list == NULL){$list='无';}
                echo json_encode(array('stat' => '200','stoptime' => $stoptime,'pax' => $pax,'list' => $list,'uname' => $username,'class' => $class,'msg' => $msg,'title' => $title,'fid' => $fid));
            }else{
                echo json_encode(array('stat' => '400','msg' => '你访问的收集表已被删除或不存在！已为你获取到最新收集表！'),JSON_UNESCAPED_UNICODE);
            }
    }elseif($fun == 'xinjian'){
        //新建
        if(!$username)
     	{
     	    echo  json_encode(array('stat' => '400','msg' => '你没有权限，请你先登录！'),JSON_UNESCAPED_UNICODE);
     	    exit();
     	}
     	
        $title = $data['title'];
        $msg = $data['msg'];
        $class = $data['class'];
        $stoptime = $data['stoptime'];
        //$username =  $data['uname'];
        $time=date("Y-m-d H:i:s");
		
		if($title !=NULL and $class !=NULL and $stoptime !=NULL and $username !=NULL){
				
			$sql = mysqli_query($conn,"SELECT uid,stat FROM users WHERE username LIKE '$username' ");
			$users = @mysqli_fetch_array($sql);
			$uid = $users['uid'];
			$ustat = $users['stat'];
			if($uid !=NULL and $ustat != '0'){
				$sql="INSERT INTO gather (title,msg,time,stoptime,uid,class) VALUES('$title','$msg','$time','$stoptime','$uid','$class')";
				$coll = mysqli_query($conn,$sql);
					
				$sql = mysqli_query($conn,"SELECT fid FROM gather WHERE title LIKE '$title' and msg LIKE '$msg'");
				$qr = mysqli_fetch_array($sql);
				$fid = $qr['fid'];
				if(!$coll or $fid==NULL){
					echo json_encode(array('stat' => '400','msg' => '创建失败，服务器内部错误！'),JSON_UNESCAPED_UNICODE);
				}
				else{
					echo json_encode(array('stat' => '200','fid' => $fid),JSON_UNESCAPED_UNICODE);
				}
			}else{
				echo json_encode(array('stat' => '400','msg' => '用户已被封禁或不存在！'),JSON_UNESCAPED_UNICODE);
			}
		}else{
			echo json_encode(array('stat' => '400','msg' => '输入的数据不能为空！'),JSON_UNESCAPED_UNICODE);
		}
    }elseif($fun =='bianji'){
        //编辑
        if(!$username)
     	{ 
			echo  json_encode(array('stat' => '400','msg' => '你没有权限，请先登录！'),JSON_UNESCAPED_UNICODE);
			exit();
     	}
        $fid = $data['fid'];
		$title = $data['title'];
		$msg = $data['msg'];
		$class = $data['class'];
		$stoptime = $data['stoptime'];
		//$username =  $data['uname'];
		
        $validWidPattern = '/^\d+$/';
        if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
        	echo  json_encode(array('stat' => '400','msg' => '非法请求，GET数据错误！'),JSON_UNESCAPED_UNICODE);
        	exit();
        } 
		
		if($title and $class and $stoptime and $username){
			$sql = mysqli_query($conn,"SELECT u.username,u.stat FROM gather f JOIN users u ON u.uid=f.uid WHERE fid LIKE '$fid'");
			$users = mysqli_fetch_array($sql);
			$user = $users['username'];
			
			if(($user == $username and $state!= '0') or $username == 'admin' or $state=='6'){
				$sql= "UPDATE gather SET title='$title',msg='$msg',class='$class',stoptime='$stoptime' WHERE fid = '$fid'";
				$gathers=$conn->query($sql);
				if($gathers){
					echo json_encode(array('stat' => '200','msg' => '修改成功！','fid' => $fid),JSON_UNESCAPED_UNICODE);
				}else{
					echo json_encode(array('stat' => '400','msg' => '更改失败，服务器发生内部错误！'),JSON_UNESCAPED_UNICODE);
				}
			}else{
				echo json_encode(array('stat' => '400','msg' => '你没有权限更改此收集表！只有原创作者才能编辑！'),JSON_UNESCAPED_UNICODE);
			}
			
		}else{
			echo json_encode(array('stat' => '400','msg' => '输入的数据不能为空！'),JSON_UNESCAPED_UNICODE);
		}
    }else if($fun == 'bianjiqingqiu'){
		//编辑请求查询
		
		if(!$username)
     	{
     	    echo  json_encode(array('stat' => '400','msg' => '你没有权限，请先登录！'),JSON_UNESCAPED_UNICODE);
     	    exit();
     	}
		$fid = $data['fid'];
		
        $validWidPattern = '/^\d+$/';
        if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
        	echo  json_encode(array('stat' => '400','msg' => '非法请求，GET数据错误！'),JSON_UNESCAPED_UNICODE);
        	exit();
        } 
        
		//$username = $data['uname'];
		$sql = "SELECT f.stoptime,u.username,f.class,f.title,f.msg,u.stat FROM gather f JOIN users u ON f.uid=u.uid WHERE f.fid LIKE ".$fid;
		$gather = mysqli_query($conn,$sql);
		$gather = mysqli_fetch_array($gather);
		if($gather){
		    $stoptime = $gather['stoptime'];//2023-04-02T19:25
		    $user = $gather['username'];
		    $class = $gather['class'];
		    $title = $gather['title'];
		    $msg = $gather['msg'];
			if($username == $user and $state!= '0' or $username == 'admin' or $state=='6'){
				echo json_encode(array('stat' => '200','stoptime'=>$stoptime,'class'=>$class,'msg'=>$msg,'title'=>$title,'fid'=>$fid),JSON_UNESCAPED_UNICODE);
			}else{
				echo json_encode(array('stat' => '400','msg' => '你没有权限更改此收集表！只有原创作者才能编辑！'),JSON_UNESCAPED_UNICODE);
			}
		}else{
		    echo json_encode(array('stat' => '400','msg' => '你访问的收集表已被删除或不存在！'),JSON_UNESCAPED_UNICODE);
		}
	}elseif($fun == 'all'){
		$fid = $data['fid'];
		
		$validWidPattern = '/^\d+$/';
        if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
        	echo  json_encode(array('stat' => '400','msg' => '非法请求，GET数据错误！'),JSON_UNESCAPED_UNICODE);
        	exit();
        } 
		if(!$username)
     	{
     	    echo  json_encode(array('stat' => '400','msg' => '你没有权限，请先登录！'),JSON_UNESCAPED_UNICODE);
     	    exit();
     	}
     	$sql = "SELECT u.username,u.stat FROM gather f JOIN users u ON f.uid=u.uid WHERE f.fid LIKE ".$fid;
    	$gather = mysqli_query($conn,$sql);
    	$gather = mysqli_fetch_array($gather);
    	if($gather){
    	    $user = $gather['username'];
    	    if($state=='6' or $state=="5" or $user == $username){}
    	    else{
    	        echo  json_encode(array('stat' => '400','msg' => '你没有权限，非创作者或管理员！'),JSON_UNESCAPED_UNICODE);
     	        exit();
    	    }
    	}
    	
		//$username = $data['uname'];
		$dir='../upfile/'.$fid;
    	$i = 1;
        if(is_dir($dir)){
            $info = opendir($dir);
            while (($file = readdir($info)) !== false) {
    		if($file!="."&&$file!=".."){
					$files[$i] = $file;
					$i++;
    			}
            }
            closedir($info);
			echo json_encode(array('stat' => '200','msg' => '获取成功！', 'files' => $files),JSON_UNESCAPED_UNICODE);
		}else{
			echo json_encode(array('stat' => '400','msg' => '无数据'),JSON_UNESCAPED_UNICODE);
		}
		
	}elseif($fun == 'lists'){
		$sql = "SELECT f.fid,f.title,u.username,f.stoptime,f.class,f.pax FROM gather f JOIN users u ON f.uid=u.uid order by fid desc LIMIT 20";
		$gathers = mysqli_query($conn, $sql);
		if ($gathers) {
			$i = 1;
			while($gather = mysqli_fetch_assoc($gathers)) {
				$title[$i] = $gather['title'];
				$user1[$i] = $gather['username'];
				$class[$i] = $gather['class'];
				$pax[$i] = $gather['pax'];
				$fid[$i] = $gather['fid'];

				$stime = $gather['stoptime'];
                $time1 = substr($stime,0,10).' '.substr($stime,11,5);
				date_default_timezone_set('PRC');
                $time2=date("Y-m-d H:i");
                $stime = substr($stime,5,2).'月'.substr($stime,8,2).'日'.substr($stime,-5,5);
                if($time1 <= $time2){$stime = '收集表已过期';}
				$stoptime[$i]= $stime;

				$i = $i+1;
			}
		}
		echo json_encode(array('stat' => '200','msg' => '获取成功！', 'title' => $title,'stoptime'=>$stoptime,'username'=>$user1,'class'=>$class,'pax'=>$pax,'fid'=>$fid),JSON_UNESCAPED_UNICODE);
	}
	else{
		echo json_encode(array('stat' => '400','msg' => '请求失败，未携带任何参数！'),JSON_UNESCAPED_UNICODE);
	}

?>